package com.example.dingtalkwebproject.controller;

import com.example.dingtalkwebproject.pojo.sysLoginUser;
import com.example.dingtalkwebproject.service.sysLoginUserService;
import com.example.dingtalkwebproject.utils.AjaxResult;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.example.dingtalkwebproject.utils.getMD5Password.customizedMD5;


@RestController
@RequestMapping("/login")
public class loginVerifyController {

    @Autowired
    private sysLoginUserService sysLoginUserService;


    @PostMapping("/checkInfo")
    private AjaxResult checkUserIsLoginSuccess(@RequestBody sysLoginUser sysLoginUser) {
        // 首先判断当前账号信息是否为空
        if (ObjectUtils.isEmpty(sysLoginUser)) {
            return AjaxResult.error("请检查当前填写参数!");
        }

        // 如果不为空 那么根据用户名获取到当前信息
        sysLoginUser loginUser = null;
        String userName = null;
        if (StringUtils.isNotBlank(sysLoginUser.getAccount())) {
            userName = sysLoginUser.getAccount().toUpperCase();
            loginUser = sysLoginUserService.selectLoginInfoByUserId(userName);
        }
        // 判断当前这个对象是否存在
        if (ObjectUtils.isEmpty(loginUser)) {
            return AjaxResult.error("当前账号不存在，请检查你的账号是否存在!");
        }

        // 如果当前账号存在，那么判断当前密码是否一致
        // 使用md5加密与原数据进行比对
        String md5Password = null;
        if (ObjectUtils.isNotEmpty(sysLoginUser.getPassword())) {
            md5Password = customizedMD5(sysLoginUser.getPassword());
        }


        if (StringUtils.isNotBlank(sysLoginUser.getAccount())) {
            // 判断账号是否相同
            if (!userName.equals(loginUser.getAccount())) {
                return AjaxResult.error("当前输入的【" + userName + "】不存在!");
            }
        }

        if (!loginUser.getPassword().equals(md5Password)) {
            return AjaxResult.error("当前输入的密码不正确，请仔细核对密码!");
        } else {
            return AjaxResult.success("登陆成功!");
        }

    }
}
